package cn.edu.ctbu.datajpa2.webapi;


import cn.edu.ctbu.datajpa2.Service.StudentService;
import cn.edu.ctbu.datajpa2.core.PageUtils;
import cn.edu.ctbu.datajpa2.domain.Student;
import cn.edu.ctbu.datajpa2.utils.RUtils;
import cn.edu.ctbu.datajpa2.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/webapi/student")
@Slf4j
public class StudentRestController {

    @Autowired
    private StudentService studentService;


    @GetMapping("/list")
    public List<Student> getAll(){

        List<Student> students=studentService.findAll();

        return students;
    }


    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "name",defaultValue = "")String name){
        
        Sort sort= Sort.by(Sort.Direction.DESC,"id");

        Page<Student> studentPage;
        if(StringUtils.isEmpty(name)){
            Pageable pageable= PageRequest.of(page,size,sort);
            studentPage=studentService.findAll(pageable);
        }else{
            Student student=new Student();
            student.setName(name);

            Pageable pageable= PageRequest.of(0,10,sort);
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Student> example=Example.of(student,matcher);
            studentPage=studentService.findAll(example,pageable);
        }
       // Pageable pageable= PageRequest.of(page,size,sort);

       // Page<Student> studentPage=studentService.findAll(pageable);

        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
       return pageUtils;
    }

    @GetMapping("/get/{id}")
    public Student get(@PathVariable Integer id){
        Student student=studentService.getById(id);
        student.setPassword("");

        return student;
    }
    @PostMapping("/insert")
    public Student insert(Student student){
        Student student1=studentService.insert(student);
        return student1;
    }
    @PutMapping("/update")
    public Student update(Student student){
        //读取旧的数据
        Student oldstudent=studentService.getById(student.getId());

        if(StringUtils.isEmpty(student.getPassword())){
            student.setPassword(oldstudent.getPassword());
        }
        student=studentService.update(student);
        return student;
    }
    @DeleteMapping ("/delete/{id}")
    public void delete(@PathVariable Integer id){
        studentService.delete(id);

    }

    @PostMapping(value = "/add")
    public R add(@Valid Student student, BindingResult bindingResult){
        if(bindingResult.hasErrors()){

            String errorMsg=bindingResult.getFieldError().getDefaultMessage();

            log.info("新增学生出错了！{}",errorMsg);
            return RUtils.error(-10,errorMsg);
        }
        //studentService.add(student);
       // Integer myid= student.getId();
        //return myid.intValue();
        studentService.insert(student);
        return RUtils.success();
    }
    @GetMapping("/list2")
    public R<Page<Student>> list(int pageIndex,int pageSize){
        Pageable pageable=PageRequest.of(pageIndex,pageSize,Sort.by("id").descending());
        return RUtils.success(studentService.findAll(pageable));
    }
}
